
package com.jf.cloud.search.controller.supplier;

import com.jf.cloud.api.dto.EsPageDTO;
import com.jf.cloud.api.product.feign.CategoryFeignClient;
import com.jf.cloud.api.vo.EsPageVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.i18n.LanguageEnum;
import com.jf.cloud.common.product.bo.EsBrandBO;
import com.jf.cloud.common.product.constant.SupplierSpuType;
import com.jf.cloud.common.product.dto.ProductSearchDTO;
import com.jf.cloud.common.product.vo.CategoryVO;
import com.jf.cloud.common.product.vo.search.SpuAdminVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.search.constant.EsConstant;
import com.jf.cloud.search.manager.ProductSearchManager;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 商品搜索
 * @author zz
 * @date 2020/11/16
 */
@RestController("suplplierSearchSpuController")
@RequestMapping("/s/search")
@Tag(name = "supplier-spu管理列表接口")
public class ProductSearchController {

    @Autowired
    private ProductSearchManager productSearchManager;
    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @GetMapping("/page")
    @Operation(summary = "商品信息列表" , description = "商品信息列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> page(@Valid EsPageDTO pageDTO, @ParameterObject ProductSearchDTO productSearchDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        productSearchDTO.setFetchSource(EsConstant.SUPPLIER_FETCH_SOURCE);
        productSearchDTO.setSupplierId(supplierId);
        productSearchDTO.setShopId(-1L);
        productSearchDTO.setSupplierSpuType(SupplierSpuType.YES.value());
        EsPageVO<SpuAdminVO> searchPage =  productSearchManager.adminPage(pageDTO, productSearchDTO);
        List<SpuAdminVO> list = searchPage.getList();
        Set<Long> categoryIds = list.stream().map(SpuAdminVO::getCategoryId).collect(Collectors.toSet());
        ServerResponseEntity<List<CategoryVO>> categoryResp = categoryFeignClient.getByCategoryIds(categoryIds);
        Map<Long, CategoryVO> categoryVOMap = categoryResp.getData().stream().collect(Collectors.toMap(CategoryVO::getCategoryId, c -> c));
        for (SpuAdminVO spuAdminVO : list) {
            //品牌名称
            setBrandName(spuAdminVO);
            spuAdminVO.setCategoryNames(Objects.nonNull(spuAdminVO.getCategoryId()) ? Objects.equals(I18nMessage.getLang(), LanguageEnum.LANGUAGE_ZH_CN.getLang()) ? categoryVOMap.get(spuAdminVO.getCategoryId()).getCategoryLangList().get(0).getName() : categoryVOMap.get(spuAdminVO.getCategoryId()).getCategoryLangList().get(1).getName() : null);
        }
        return ServerResponseEntity.success(searchPage);

    }

    @GetMapping("/page_distribution_spu")
    @Operation(summary = "分销商品列表" , description = "分销商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageDistributionSpu(@Valid EsPageDTO pageDTO, @ParameterObject ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            productSearchDTO.setShopId(shopId);
        }
        productSearchDTO.setFetchSource(EsConstant.DISTRIBUTION_SPU_SOURCE);
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        List<SpuAdminVO> list = searchPage.getList();
        return ServerResponseEntity.success(searchPage);
    }

    public void setBrandName(SpuAdminVO spuAdminVO){
        //品牌名称
        Integer lang = I18nMessage.getLang();
        EsBrandBO brand = spuAdminVO.getBrand();
        if (!Objects.isNull(brand)){
            if (Objects.equals(lang, LanguageEnum.LANGUAGE_ZH_CN.getLang())){
                spuAdminVO.setBrandName(brand.getBrandNameZh());
            }
            if (Objects.equals(lang,LanguageEnum.LANGUAGE_EN.getLang())){
                spuAdminVO.setBrandName(brand.getBrandNameEn());
            }
        }
    }
}
